package redis

import (
	"time"
)

// 短信写入缓存
func ResisterSmsCode(tel, code, source string) error {
	key := "sms:" + "tel" + tel + source
	return RedClient.Set(key, code, time.Minute*10).Err()
}

// 读取缓存中的短信
func GetSmsCode(tel, source string) string {
	key := "sms:" + "tel" + tel + source
	return RedClient.Get(key).Val()
}

// 短信限流 10分钟3次
func SendSmsLimit(tel string) bool {
	key := "sms_num:tel_" + tel
	if RedClient.Get(key).Val() == "" { //判断此号码有没有发送过
		RedClient.Set(key, "0", time.Minute*10)
	}

	if RedClient.Get(key).Val() < "3" { //判断值是否小于3
		RedClient.Incr(key) //小于的话 自增
		return true
	}
	return false
}
